package com.icow.wms.basic.controller;

import com.alibaba.fastjson.JSON;
import com.icow.wms.basic.domain.*;
import com.icow.wms.basic.service.IAgvTaskService;
import com.icow.wms.basic.service.impl.OrderTypeServiceImpl;
import com.icow.wms.basic.utils.OpenTcs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Controller
@SuppressWarnings("all")
@RequestMapping("/api/outer")
public class OuterController {
    @Autowired
    private IAgvTaskService agvTaskService;

    @Resource(name = "locMapCache")
    private Cache locMapCache;

    @Autowired
    private OrderTypeServiceImpl orderTypeService;

    @PostMapping("/createTask/{taskNo}")
    @ResponseBody
    public String craeteTask(@PathVariable String taskNo, @RequestBody TransportOrderCreationTO transportOrderCreationTO) {
        String startLocationName = transportOrderCreationTO.getDestinations().get(0).getLocationName();
        String endLocationName = transportOrderCreationTO.getDestinations().get(1).getLocationName();
        OrderType orderTypes = orderTypeService.selectOrderTypeListByLoc(startLocationName, endLocationName);
        String type = "-";
        if (orderTypes != null) {
            type = orderTypes.getOrderType();
        }
        String transportOrderString = OpenTcs.createTransportOrder(taskNo, type, transportOrderCreationTO, locMapCache);
        if (transportOrderString != null && transportOrderString.contains("true")) {
            AgvTask agvTask = new AgvTask();
            agvTask.setOuterTaskName(taskNo);
            agvTask.setInnerTaskName(taskNo);
            agvTask.setStart(startLocationName);
            agvTask.setEnd(endLocationName);
            agvTask.setTaskJson(JSON.toJSONString(transportOrderCreationTO));
            agvTask.setStartTime(new Date());
            agvTask.setStatus("未执行");
            agvTaskService.insertAgvTask(agvTask);
            return "{\"result\":\"success\"}";
        }
        return "{\"failed\":\"" + transportOrderString + "\"}";
    }

    @ResponseBody
    @GetMapping("/testCache/{cacheName}")
    public Cache testCache(@PathVariable String cacheName) {
        LocationMap b1024 = locMapCache.get(cacheName, LocationMap.class);
        if (b1024 != null) {
            System.out.println(b1024.getId() + " " + b1024.getOuterLocation() + " " +
                    b1024.getInnerLocation() + " " + b1024.getInnerCargo() + " ");
        }
        return locMapCache;
    }

    @ResponseBody
    @PostMapping("/createAGVTask")
    public WcsTaskResponse sendTaskByThirdParty(@RequestBody WcsTask task) {
        log.info(task + "");
        if (task == null) {
            return new WcsTaskResponse(400, "", "无请求参数");
        }
        if (task.getFromPort() == null || task.getTaskNo() == null || task.getTaskType() == null) {
            return new WcsTaskResponse(400, "", "请求参数为空");
        }
        OrderType orderTypes = orderTypeService.selectOrderTypeListByLoc(task.getFromPort(), task.getToPort());
        String type = "-";
        if (orderTypes != null) {
            type = orderTypes.getOrderType();
        }
        String createTransportOrderString = OpenTcs.createTransportOrder(task.getTaskNo(), task.getPreTaskNo(),
                task.getPriority(), task.getFromPort(), task.getToPort(), locMapCache, type);
        if (createTransportOrderString == null || !createTransportOrderString.contains("true")) {
            return new WcsTaskResponse(400, "", "任务创建失败:" + createTransportOrderString);
        }
        AgvTask agvTask = new AgvTask();
        agvTask.setOuterTaskName(task.getTaskNo());
        agvTask.setInnerTaskName(task.getTaskNo());
        agvTask.setStart(task.getFromPort());
        agvTask.setEnd(task.getToPort());
        agvTask.setTaskJson(JSON.toJSONString(task));
        agvTask.setStartTime(new Date());
        agvTask.setStatus("未执行");
        agvTaskService.insertAgvTask(agvTask);
        return new WcsTaskResponse(200, "", "任务创建成功");
    }


}
